package com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyReProf.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyReProf.repository.HddcWyGeomorphysvyreprofNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyReProf.repository.HddcWyGeomorphysvyreprofRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyReProf.repository.entity.HddcWyGeomorphysvyreprofEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyReProf.service.HddcWyGeomorphysvyreprofService;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyReProf.viewobjects.HddcWyGeomorphysvyreprofQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyReProf.viewobjects.HddcWyGeomorphysvyreprofVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lihelei
 * @date 2020-12-01
 */
@Service
public class HddcWyGeomorphysvyreprofServiceImpl implements HddcWyGeomorphysvyreprofService {

	@Autowired
    private HddcWyGeomorphysvyreprofRepository hddcWyGeomorphysvyreprofRepository;
    @Autowired
    private HddcWyGeomorphysvyreprofNativeRepository hddcWyGeomorphysvyreprofNativeRepository;

    @Override
    public JSONObject queryHddcWyGeomorphysvyreprofs(HddcWyGeomorphysvyreprofQueryParams queryParams , int curPage,
                                                     int pageSize) {
        Page<HddcWyGeomorphysvyreprofEntity> hddcWyGeomorphysvyreprofPage =
                this.hddcWyGeomorphysvyreprofNativeRepository.queryHddcWyGeomorphysvyreprofs(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeomorphysvyreprofPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeomorphysvyreprofEntity getHddcWyGeomorphysvyreprof(String id) {
        HddcWyGeomorphysvyreprofEntity hddcWyGeomorphysvyreprof = this.hddcWyGeomorphysvyreprofRepository.findById(id).orElse(null);
         return hddcWyGeomorphysvyreprof;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorphysvyreprofEntity saveHddcWyGeomorphysvyreprof(HddcWyGeomorphysvyreprofEntity hddcWyGeomorphysvyreprof) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeomorphysvyreprof.setId(uuid);
        hddcWyGeomorphysvyreprof.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorphysvyreprof.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeomorphysvyreprofRepository.save(hddcWyGeomorphysvyreprof);
        return hddcWyGeomorphysvyreprof;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorphysvyreprofEntity updateHddcWyGeomorphysvyreprof(HddcWyGeomorphysvyreprofEntity hddcWyGeomorphysvyreprof) {
        hddcWyGeomorphysvyreprof.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorphysvyreprof.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeomorphysvyreprofRepository.save(hddcWyGeomorphysvyreprof);
        return hddcWyGeomorphysvyreprof;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeomorphysvyreprofs(List<String> ids) {
        List<HddcWyGeomorphysvyreprofEntity> hddcWyGeomorphysvyreprofList = this.hddcWyGeomorphysvyreprofRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeomorphysvyreprofList) && hddcWyGeomorphysvyreprofList.size() > 0) {
            for(HddcWyGeomorphysvyreprofEntity hddcWyGeomorphysvyreprof : hddcWyGeomorphysvyreprofList) {
                this.hddcWyGeomorphysvyreprofRepository.delete(hddcWyGeomorphysvyreprof);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeomorphysvyreprof(HddcWyGeomorphysvyreprofQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeomorphysvyreprofNativeRepository.queryHddcWyGeomorphysvyreprof(queryParams);
        return bigInteger;
    }

}
